package data;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class MemberDataBase {
        
        public static  MemberDataBase usersDataBase;
        
        private Map<String,String> userToPassword;
		private Map<String,Member> users;
        private Vector<Member> loginUsers;
        private int globalId;
        
        private MemberDataBase(){
                this.userToPassword = new HashMap<String, String>();
                this.users = new HashMap<String,Member>();
                this.loginUsers = new Vector<Member>();
                globalId =1;
                
        }
        
        public Member getMember(String userName)
        {
        	return users.get(userName);
        }
        
        public boolean addNewMember(String userName,String password){
                boolean ans = false;
                if (userToPassword.get(userName)==null){
                        userToPassword.put(userName, password);
                        users.put(userName,new Member(userName, getGlobalID()));
                        ans = true;
                }
                return ans;
        }
        
        public synchronized Integer getGlobalID(){
        	Integer tmp=this.globalId;
        	this.globalId++;
        	return tmp;
        }
        
        public boolean authenticate(String userName,String password){
                boolean ans = false;
                String realPassword = userToPassword.get(userName);
                if (realPassword !=null){
                        ans = realPassword.equals(password);
                }
                return ans;
        }
        
        public static MemberDataBase getMembersDataBase()
        {
                if (usersDataBase == null)
                {
                        usersDataBase = new MemberDataBase();
                }
                return usersDataBase;
        }

		public void addLoginMember(Member u) {
			
			this.loginUsers.add(u);
			
		}
		
		public void removeLoginMember(Member u){
			this.loginUsers.remove(u);
		}
		
/**
 * what is your role in the current path
 * @param userName
 * @param path vector[0] =subforumID vector[0] =messageID
 * @return user's role: guest/member/owner/moderator/admin
 */
		public static String getUserRoll(String userName, Vector<Integer> path) {//need to Implement
			Member user = MemberDataBase.getMembersDataBase().getUsers().get(userName);
			String answer= getUserRoll(userName);
			if(answer.equalsIgnoreCase("admin") || answer.equalsIgnoreCase("guest"))
				return answer;
			Integer subForumID = path.get(0);
			if(user.isModerator(subForumID))
				return "moderator";
			if(path.size() > 1 && user.ownMessage(subForumID,path.get(1)))
				return "owner";
			return "member";
		}
/**
 * what is your general role
 * @param userName user name 
 * @return user's role: guest/member/admin
 */
		public static String getUserRoll(String userName) {//need to Implement
			Member user = MemberDataBase.getMembersDataBase().getUsers().get(userName);
			if(user==null)
				return "guest";
			if (user.get_isAdmin())
				return "admin";
			return "member";
		}

/**
 * @return the userToPassword
 */
public Map<String, String> getUserToPassword() {
	return userToPassword;
}

/**
 * @return the users
 */
public Map<String, Member> getUsers() {
	return users;
}

/**
 * @return the loginUsers
 */
public Vector<Member> getLoginUsers() {
	return loginUsers;
}
        
		
}
